<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery-3.7.1.min.js"></script>
    <link rel="stylesheet" href="/plugins/bootstraptable/bootstrap-table.min.css">
    <link rel="stylesheet" href="/css/errorcss.css">


</head>
<body>
<div class="container-fluid">
    <div style="font-size: xx-large;text-align: center">学生管理页面</div>


    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" onclick="preAdd();">
        新增学生
    </button>
    <button type="button" class="btn btn-danger btn-lg" data-toggle="modal" data-target="#myModal" onclick="batchRemove()">批量删除学生</button>



    <form class="form-inline">
        <div class="form-group">
            <label for="searchName">请输入名字</label>
            <input type="text" class="form-control" id="searchName" placeholder="请输入姓名" name="searchName">
        </div>
        <div class="form-group">
            <label for="searchNo">请输入学号</label>
            <input type="text" class="form-control" id="searchNo" placeholder="请输入学号" name="searchNo">
        </div>
        <div class="form-group">
            <label for="searchSex">请选择性别</label>
            <select class="form-control" id="searchSex" name="searchSex">
                <option value="">请选择</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>
        <button type="button" class="btn btn-default btn-primary" onclick="searchByCondition()">查询</button>
    </form>


    <table class="table table-bordered" id="stuTable">

    </table>

</div>

<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <form id="formStudent">
            <div class="modal-body">


                    <input hidden="hidden" id="id" name="id"/>

                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名" required
                               minlength="2" maxlength="20">
                    </div>
                    <div class="form-group">
                        <label for="no">学号</label>
                        <input type="text" class="form-control" id="no" name="no" placeholder="学号" required
                               minlength="10" maxlength="10">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="年龄" required
                               min="16" max="30">
                    </div>

                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="成绩" required
                               min="0" max="100">
                    </div>
            </div>
            <div class="modal-footer">

                <!--<button type="button" class="btn btn-primary" onclick="saveStudent();">保存</button>-->
                <button type="submit" class="btn btn-primary">提交更改</button>
            </div>
            </form>
            <!--表单结束-->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/plugins/jqvalidate/jquery.validate.min.js"></script>
<script src="/plugins/jqvalidate/localization/messages_zh.min.js"></script>



<script type="text/javascript">


    function loadTable(){

        $('#stuTable').bootstrapTable('destroy'); // 先销毁表格

        $('#stuTable').bootstrapTable({

            url: '/webapi/student/getbypage',
            striped:true,//是否由隔行变色效果
            pagination:true,//是否分页
            singleSelect: false,//设置为true将禁止多选
            pageSize:3,//每页的数据跳条数
            pageNumber:1,//如果设置了分页，首页的页码
            sidePagination: "server",//设置在哪里进行分页，可选为client或server
            queryParams:function (params) {
                var paraObj={
                    size: params.limit,
                    page:params.offset/params.limit,
                    sort:'id',
                    direct:'desc',
                    name:$("#searchName").val(),
                    no:$("#searchNo").val(),
                    sex:$("#searchSex").val()

                };

                return paraObj;

            },
            columns: [{
                checkbox: true,
                field: 'state',
                title: '选择'
            }, {
                field: 'id',
                title: '序号'
            }, {
                field: 'name',
                title: '姓名'
            }, {
                field: 'no',
                title: '学号'
            },{
                field: 'sex',
                title: '性别',
                formatter: function (value) {
                    if (value === 1) {
                        return '男';
                    } else if (value === 2) {
                        return '女';
                    } else if (value === 0) {
                        return '未知';
                    } else {
                        return '';
                    }
                }
            }, {
                field: 'score',
                title: '成绩'
            }, {
                field: 'operate',
                title: '操作',
                formatter: function (value, row, index) {
                    return '<a href="#" onclick="editStudent(' + row.id + ');">编辑&nbsp&nbsp</a> ' +
                        '<a href="#" onclick="deleteStudent(' + row.id + ');">&nbsp&nbsp删除</a>';
                }
            }]
        });
    }

    //新增或更新
    function saveStudent() {

        var data=$("#formStudent").serialize();

        var id=$("#id").val();
        if (id<1){
            //新增
            $.ajax({

                url: "/webapi/student/insert",
                method:"post",

                data:data


            }).done(function (){

                loadTable();

                $("#myModal").modal("hide");

            })
        }else{
            //更新
            $.ajax({

                url: "/webapi/student/update",
                method:"put",

                data:data


            }).done(function (){

                loadTable();

                $("#myModal").modal("hide");

            })
        }

    }

    function editStudent(id) {

        $("#myModal").modal("show");

        $.ajax({
            url:"/webapi/student/get/"+id
        }).done(function (rs) {

            $("#id").val(rs.id);
            $("#name").val(rs.name);
            $("#password").val("");
            $("#sex").val(rs.sex);
            $("#score").val(rs.score);

        })

    }

    function preAdd() {

        $("#id").val(0);//提示新增

    }

    function deleteStudent(id) {

        if (confirm("确认是否删除")){
            $.ajax({

                url:"/webapi/student/delete/"+id,
                method:"delete"
            }).done(function () {

                loadTable();

            });
        }

    }

    function searchByName() {
        loadTable();
    }

    function searchByCondition() {
        loadTable();
    }

    /*function batchRemove() {
        var selectedRows = $('#stuTable').bootstrapTable('getSelections');
        var ids = selectedRows.map(function (row) {
            return row.id;
        });
        if (ids.length === 0) {
            alert('请选择要删除的学生');
            return;
        }
        if (confirm('确认是否删除所选学生？')) {
            $.ajax({
                url: '/webapi/student/batchRemove',
                method: 'post',
                data: {
                    ids: ids
                }
            }).done(function () {
                loadTable();
            });
        }
    }*/




    $(function () {
        $('#formStudent').validate({
            submitHandler:saveStudent
        });

        loadTable();
    })
</script>
</body>
</html>